package org.zero.db;

import lombok.experimental.UtilityClass;

import java.lang.reflect.Method;
import java.util.Properties;

/**
 * @author Zero (cnzeropro@qq.com)
 * @date 2021/8/18 13:24
 */
@UtilityClass
public class ServiceMethodHelper {
    private static final Properties PROPERTIES = new Properties();

    static {
        try {
            PROPERTIES.load(ServiceMethodHelper.class.getClassLoader().getResourceAsStream("method.properties"));
        } catch (Exception e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    public boolean isSaveMethod(Method method) {
        String property = PROPERTIES.getProperty("saveMethodPrefix");
        return judgeMethod(method, property);
    }

    public boolean isUpdateMethod(Method method) {
        String property = PROPERTIES.getProperty("updateMethodPrefix");
        return judgeMethod(method, property);
    }

    public boolean isRemoveMethod(Method method) {
        String property = PROPERTIES.getProperty("removeMethodPrefix");
        return judgeMethod(method, property);
    }

    public boolean isQueryMethod(Method method) {
        String property = PROPERTIES.getProperty("queryMethodPrefix");
        return judgeMethod(method, property);
    }

    public boolean isChangeMethod(Method method) {
        return isSaveMethod(method) || isUpdateMethod(method) || isRemoveMethod(method);
    }

    public boolean isServiceMethod(Method method) {
        return isChangeMethod(method) || isQueryMethod(method);
    }

    private boolean judgeMethod(Method method, String property) {
        String name = method.getName();
        String[] split = property.split(",");
        for (String s : split) {
            if (name.contains(s)) {
                return true;
            }
        }
        return false;
    }
}
